package com.china.lottery.listener.jczq.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import com.mina.rbc.util.DateUtil;
import com.mina.rbc.util.StringUtil;

public class JinCaiStringUtil {

	//public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String[] WEEK_PHRASE = new String[]{"周日","周一","周二","周三","周四","周五","周六"};
	
	/**
	 * 正确的场次转换
	 * @param bdate
	 * @param iname
	 * @return
	 */
	public static String convertItemNameToID(Date bdate,String iname) {
		String week=iname.substring(0, 2); 
		String w="1";
		   switch (week) {
			           case "周一":
				            w="1";
				             break;
				        case"周二":
				        	 w="2";
				             break;
				         case "周三":
				        	 w="3";
				             break;
				         case "周四":
				        	 w="4";
				             break;
				         case "周五":
				        	 w="5";
				             break;
				         case "周六":
				        	 w="6";
				             break;
				             default:
				             w="7";
				         }
		      
		long ltime = bdate.getTime();
		Calendar cal  = Calendar.getInstance(Locale.CHINA);
		cal.setTimeInMillis(ltime);
		int weekNo = cal.get(Calendar.DAY_OF_WEEK)-1;
		String phrase = iname.replaceAll("\\d", "").trim();
		int wn = -1;
		for (int j = 0; j < WEEK_PHRASE.length; j++) {
			if (phrase.equalsIgnoreCase(WEEK_PHRASE[j])) {
				wn = j;
			}
		}

		int diff = 0;
		if ( wn > weekNo ) {
			diff = wn - weekNo;
		} else if ( wn == weekNo ) {
			diff = 0;
		} else {
			diff = 7 - (weekNo - wn);
		}
		cal.add(Calendar.DAY_OF_MONTH, diff);
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		String tid = cal.get(cal.YEAR)+sdf.format(cal.getTime()).substring(2)+w+ iname.replaceAll("\\D", "").trim();;
		return tid;
	}
	/**
	 * 正确的场次转换
	 * @param bdate
	 * @param iname
	 * @return
	 */
	public static String convertItemNameToIDS(String date,String iname) {
		  String week=iname.substring(0, 2); //2018-03-10 01:30:00
		  String w="1";
		   switch (week) {
			           case "周一":
				            w="1";
				             break;
				        case"周二":
				        	 w="2";
				             break;
				         case "周三":
				        	 w="3";
				             break;
				         case "周四":
				        	 w="4";
				             break;
				         case "周五":
				        	 w="5";
				             break;
				         case "周六":
				        	 w="6";
				             break;
				             default:
				             w="7";
				         }
		String date2=date.substring(0, 10).replaceAll("-", "");
		Calendar cal  = Calendar.getInstance(Locale.CHINA);
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		String tid=date2+w+iname.substring(2);
		
		return tid;
	}
	
	public static String convert2MatchTime(String mt,int m) {
		Date dtime = DateUtil.parserDateTime("20" + mt + ":00");
		long ltime = dtime.getTime() + 1000 * 60 * m;
		
		if (mt.endsWith("23:59")) {
			ltime = dtime.getTime();
		}
		String sreal = DateUtil.getDateTime(ltime, "yyyy-MM-dd HH:mm:ss");
		return sreal;
	}

	public final static String getRealEndTime(String endTime) {
		String s = endTime;
		String[] ss = StringUtil.splitter(s, " ");

		int day = 0;
		int wd = -1;
		String sreal = "";
		Date dtime = DateUtil.parserDateTime("20" + s + ":00");

		Calendar nowcal = Calendar.getInstance(Locale.CHINA);
		if (ss[1].compareToIgnoreCase("12:00") >= 0) {
			long adjust = 1000 * 60 * 1;
			nowcal.setTime(dtime);
			sreal = "20" + ss[0] + " " + ss[1] + ":00";
			Date dtime1 = DateUtil.parserDateTime(sreal);
			dtime.setTime(dtime1.getTime() - adjust);

		} else {
			long adjust = 0;
			nowcal.setTimeInMillis(dtime.getTime() - 1000 * 60 * 60 * 24);
			wd = nowcal.get(Calendar.DAY_OF_WEEK);
			if (wd == Calendar.SUNDAY || wd == Calendar.SATURDAY) {
				if (ss[1].compareToIgnoreCase("09:30") > 0) {
					adjust = 1000 * 60 * 1;
				} else {
					if ( ss[0].compareToIgnoreCase("14-06-12")  > 0 && ss[0].compareToIgnoreCase("14-06-13") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-13")  > 0 && ss[0].compareToIgnoreCase("14-06-14") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("06:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "05:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-14")  > 0 && ss[0].compareToIgnoreCase("14-06-15") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("09:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "08:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-15")  > 0 && ss[0].compareToIgnoreCase("14-06-23") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("06:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "05:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-23")  > 0 && ss[0].compareToIgnoreCase("14-06-27") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-27")  > 0 && ss[0].compareToIgnoreCase("14-06-28") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							day = 1;
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-28")  > 0 && ss[0].compareToIgnoreCase("14-07-02") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-02")  > 0 && ss[0].compareToIgnoreCase("14-07-04") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-04")  > 0 && ss[0].compareToIgnoreCase("14-07-06") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-06")  > 0 && ss[0].compareToIgnoreCase("14-07-08") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-08")  > 0 && ss[0].compareToIgnoreCase("14-07-10") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-10")  > 0 && ss[0].compareToIgnoreCase("14-07-12") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-12")  > 0 && ss[0].compareToIgnoreCase("14-07-13") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-13")  > 0 && ss[0].compareToIgnoreCase("14-07-14") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("03:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "02:59";
						}
					} else {
						if (ss[1].compareToIgnoreCase("01:00") <= 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "00:59";
						}
					}
				}
			} else {
				if (ss[1].compareToIgnoreCase("09:30") > 0) {
					adjust = 1000 * 60 * 1;
				} else {
					if ( ss[0].compareToIgnoreCase("14-06-12")  > 0 && ss[0].compareToIgnoreCase("14-06-13") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-13")  > 0 && ss[0].compareToIgnoreCase("14-06-14") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("06:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "05:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-14")  > 0 && ss[0].compareToIgnoreCase("14-06-15") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("09:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "08:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-15")  > 0 && ss[0].compareToIgnoreCase("14-06-23") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("06:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "05:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-23")  > 0 && ss[0].compareToIgnoreCase("14-06-27") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-27")  > 0 && ss[0].compareToIgnoreCase("14-06-28") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							day = 1;
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-06-28")  > 0 && ss[0].compareToIgnoreCase("14-07-02") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-02")  > 0 && ss[0].compareToIgnoreCase("14-07-04") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-04")  > 0 && ss[0].compareToIgnoreCase("14-07-06") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-06")  > 0 && ss[0].compareToIgnoreCase("14-07-08") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-08")  > 0 && ss[0].compareToIgnoreCase("14-07-10") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-10")  > 0 && ss[0].compareToIgnoreCase("14-07-12") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("00:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							day = 1;
							ss[1] = "23:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-12")  > 0 && ss[0].compareToIgnoreCase("14-07-13") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("04:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "03:59";
						}
					} else if ( ss[0].compareToIgnoreCase("14-07-13")  > 0 && ss[0].compareToIgnoreCase("14-07-14") <= 0 ) {//世界杯期�?
						if (ss[1].compareToIgnoreCase("03:00") < 0) {
							adjust = 1000 * 60 * 1;
						} else {
							ss[1] = "02:59";
						}
					} else {
						day = 1;
						ss[1] = "23:59";
					}
				}
			}

			sreal = "20" + ss[0] + " " + ss[1] + ":00";
			dtime = DateUtil.parserDateTime(sreal);
			dtime.setTime(dtime.getTime() - day * 1000 * 60 * 60 * 24 - adjust);
		}
		Date startDate = DateUtil.parserDateTime("2013-02-08 23:00:00");
		Date endDate = DateUtil.parserDateTime("2013-02-16 00:00:00");
		if(dtime.after(startDate) && dtime.before(endDate)){
			dtime = DateUtil.parserDateTime("2013-02-08 22:59:00");
		}
		return DateUtil.getDateTime(dtime.getTime(), "yyyy-MM-dd HH:mm:ss");
	}
}
